package com.briup.corejava.day16;

/**
 * join方法中的案例
 * join() ：无限期等待
 * join(毫秒时间) ：计时等待
 */
public class Test1 {
    public static void main(String[] args) {
        Thread t1 = new Thread("th1"){
            @Override
            public void run() {
                for (int i = 1; i <41 ; i++) {
                    System.out.println(i+"-"+Thread.currentThread().getName());
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Thread t2 = new Thread("th2"){
            @Override
            public void run() {
                for (int i = 100; i >=70 ; i--) {
                    System.out.println(Thread.currentThread().getName()+" - "+i);
                    // 当输出到80的时候，t1加入
                    if(i == 80){
                        try {
                            // 使用join
                            t1.join(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        // 启动线程
        t1.start();
        t2.start();
        // 主线程每隔7ms输出t2的状态
        while(true){
            Thread.State state = t2.getState();
            System.out.println(state.name());
            try {
                Thread.sleep(7);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
